package game.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.*;

import game.model.Game;

/**
 * @author Aqilah
 * Class SettingsDialog display settings dialog that allows user to change the settings
 */
public class SettingsDialog extends JDialog implements ActionListener {
	private JTextField txtboardSize;
	private JRadioButton rdoEasy;
	private JRadioButton rdoMedium;
	private JRadioButton rdoHard;
	private JRadioButton rdoColorBlack;
	private JRadioButton rdoColorWhite;
	private JRadioButton rdoColorBlue;
	private JRadioButton rdoColorGray;
	private JRadioButton rdoColorGreen;
	private JRadioButton rdoEnglish;
	private JRadioButton rdoSpanish;
	private JRadioButton rdoFrench;
	private JButton btnOK;

	public int boardSize;
	public int gameMode;
	public Color color;
	public String language;

	/** Constructor */
	public SettingsDialog(JFrame parent) {
		super(parent, true);
		setTitle("Settings");
		createGUI();
	}

	/** Create GUI for the dialog */
	public void createGUI() {
		setLayout(new BorderLayout());
		JTabbedPane tabbedPane = new JTabbedPane();		

		// default
		tabbedPane.addTab("Default Settings", makeDefaultPanel());
		tabbedPane.addTab("Language", makeLanguagePanel());
		tabbedPane.addTab("Background", makeBackgroundPanel());
		
		add(tabbedPane, BorderLayout.CENTER);
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 

		JPanel pnlButton = new JPanel();
		btnOK = new JButton("OK");
		btnOK.addActionListener(this);
		pnlButton.add(btnOK);
		add(pnlButton, BorderLayout.SOUTH);

		setBounds(100, 100, 400, 400);
		
		// update labels
		ResourceBundle rb = ResourceBundle.getBundle("languages.MessagesBundle", getParent().getLocale());
		setTitle(rb.getString("settings"));
		tabbedPane.setTitleAt(0, rb.getString("defaultsettings"));
		tabbedPane.setTitleAt(1, rb.getString("language"));
		tabbedPane.setTitleAt(2, rb.getString("background"));
		btnOK.setText(rb.getString("ok"));
	}
	
	/** Create default panel */
	private JComponent makeDefaultPanel(){
		JPanel panel = new JPanel(null);
		
		JLabel lblSize = new JLabel("Size of the Board:");
		lblSize.setBounds(30, 70, 150, 30);
		panel.add(lblSize);
		JLabel lblIns = new JLabel("(Even number e.g. 4)");
		lblIns.setBounds(30, 100, 200, 30);
		panel.add(lblIns);
		txtboardSize = new JTextField("4");
		txtboardSize.setBounds(150, 70, 100, 30);
		panel.add(txtboardSize);

		JLabel lblGameMode = new JLabel("Game Mode:");
		lblGameMode.setBounds(30, 150, 150, 30);
		panel.add(lblGameMode);
		rdoEasy = new JRadioButton("Easy");
		rdoEasy.setBounds(150, 150, 80, 30);
		panel.add(rdoEasy);

		rdoMedium = new JRadioButton("Medium");
		rdoMedium.setBounds(150, 180, 80, 30);
		panel.add(rdoMedium);

		rdoHard = new JRadioButton("Hard");
		rdoHard.setBounds(150, 210, 80, 30);
		panel.add(rdoHard);

		ButtonGroup bg = new ButtonGroup();
		bg.add(rdoEasy);
		bg.add(rdoMedium);
		bg.add(rdoHard);
		
		rdoEasy.setSelected(true);
		
		// update labels
		ResourceBundle rb = ResourceBundle.getBundle("languages.MessagesBundle", getParent().getLocale());
		lblSize.setText(rb.getString("sizeboard"));
		lblIns.setText(rb.getString("sizeins"));
		lblGameMode.setText(rb.getString("gamemode"));
		rdoEasy.setText(rb.getString("easy"));
		rdoMedium.setText(rb.getString("medium"));
		rdoHard.setText(rb.getString("hard"));
		
		return panel;
	}

	/** Create language panel */
	private JComponent makeLanguagePanel(){
		JPanel panel = new JPanel(null);
		
		JLabel lblChooseLanguage = new JLabel("Choose your language:");
		lblChooseLanguage.setBounds(30, 70, 150, 30);
		panel.add(lblChooseLanguage);

		rdoEnglish = new JRadioButton("English");
		rdoEnglish.setBounds(150, 150, 80, 30);
		panel.add(rdoEnglish);

		rdoSpanish = new JRadioButton("Spanish");
		rdoSpanish.setBounds(150, 180, 80, 30);
		panel.add(rdoSpanish);

		rdoFrench = new JRadioButton("French");
		rdoFrench.setBounds(150, 210, 80, 30);
		panel.add(rdoFrench);

		ButtonGroup bg = new ButtonGroup();
		bg.add(rdoEnglish);
		bg.add(rdoSpanish);
		bg.add(rdoFrench);
		
		rdoEnglish.setSelected(true);
		
		// update labels
		ResourceBundle rb = ResourceBundle.getBundle("languages.MessagesBundle", getParent().getLocale());
		lblChooseLanguage.setText(rb.getString("chooselanguage"));
		rdoEnglish.setText(rb.getString("english"));
		rdoFrench.setText(rb.getString("french"));
		rdoSpanish.setText(rb.getString("spanish"));
		
		return panel;
	}

	/** Create background panel */
	private JComponent makeBackgroundPanel(){
		JPanel panel = new JPanel(null);
		
		JLabel lblChooseColor = new JLabel("Choose background color:");
		lblChooseColor.setBounds(30, 70, 200, 30);
		panel.add(lblChooseColor);
		
		rdoColorBlack = new JRadioButton("Black");
		rdoColorBlack.setBounds(150, 100, 80, 30);
		panel.add(rdoColorBlack);

		rdoColorWhite = new JRadioButton("White");
		rdoColorWhite.setBounds(150, 130, 80, 30);
		panel.add(rdoColorWhite);

		rdoColorBlue = new JRadioButton("Blue");
		rdoColorBlue.setBounds(150, 160, 80, 30);
		panel.add(rdoColorBlue);
		
		rdoColorGray = new JRadioButton("Gray");
		rdoColorGray.setBounds(150, 190, 80, 30);
		panel.add(rdoColorGray);

		rdoColorGreen = new JRadioButton("Green");
		rdoColorGreen.setBounds(150, 220, 80, 30);
		panel.add(rdoColorGreen);

		ButtonGroup bg = new ButtonGroup();
		bg.add(rdoColorBlack);
		bg.add(rdoColorWhite);
		bg.add(rdoColorBlue);
		bg.add(rdoColorGray);
		bg.add(rdoColorGreen);
		
		rdoColorGray.setSelected(true);
		
		// update labels
		ResourceBundle rb = ResourceBundle.getBundle("languages.MessagesBundle", getParent().getLocale());
		lblChooseColor.setText(rb.getString("choosecolor"));
		rdoColorBlack.setText(rb.getString("black"));
		rdoColorWhite.setText(rb.getString("white"));
		rdoColorBlue.setText(rb.getString("blue"));
		rdoColorGray.setText(rb.getString("gray"));
		rdoColorGreen.setText(rb.getString("green"));
		
		return panel;
	}

	/** Process data when user click OK button */
	public void ok() {
		// validate input
		try{
			boardSize = Integer.parseInt(txtboardSize.getText());
			if (boardSize < 2 || boardSize > 6 || boardSize % 2 == 1){
				JOptionPane.showMessageDialog(this, "Please enter board size as an even integer in range 2 - 6");
				return;
			}
		}catch(NumberFormatException ex){
			JOptionPane.showMessageDialog(this, "Please enter board size as an integer");
			return;
		}
		
		// set mode
		if (rdoEasy.isSelected())
			gameMode = Game.EASY;
		else if (rdoMedium.isSelected())
			gameMode = Game.MEDIUM;
		else
			gameMode = Game.HARD;
		
		// set language
		if (rdoEnglish.isSelected())
			language = Game.ENGLISH;
		else if (rdoSpanish.isSelected())
			language = Game.SPANISH;
		else
			language = Game.FRENCH;
		
		// set color
		if (rdoColorBlack.isSelected())
			color = Color.BLACK;
		else if (rdoColorWhite.isSelected())
			color = Color.WHITE;
		else if (rdoColorBlue.isSelected())
			color = Color.BLUE;
		else if (rdoColorGray.isSelected())
			color = Color.GRAY;
		else
			color = Color.GREEN;
		
		this.setVisible(false);
	}

	/** Implement ActionListener interface */
	public void actionPerformed(ActionEvent arg0) {
		Object button = arg0.getSource();
		if (button == btnOK)
			ok();
	}

}
